package xyz.scoder.test;

import java.util.Observable;

public class WeatherData extends Observable {

    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() { }

    public void measurementChanged() {
        // 要求观察者自己拉取数据
        notifyObservers();
        // 主动推送数据
//        Data data = new Data();
//        data.setTemperature(temperature);
//        data.setHumidity(humidity);
//        data.setPressure(pressure);
//        notifyObservers(data);
    }

    /**
     * 3个参数实际上应该是从服务器或检测器获取的数据
     */
    public void setMeasurements(float temperature, float humidity, float pressure) {
        /**
         * 偏差超过0.5 才真正向观察者更新一次数据
         */
        if (Math.abs(temperature-this.temperature)>=0.5 || Math.abs(humidity-this.humidity)>=0.5 || Math.abs(pressure-this.pressure)>=0.5) {
            setChanged();
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            measurementChanged();
        }
    }

    public float getTemperature() { return temperature; }

    public float getHumidity() {
        return humidity;
    }

    public float getPressure() {
        return pressure;
    }
}
